import 'package:mobx/mobx.dart';
import 'package:thal/backstage/blueprint/user_print.dart';

part 'user_room.g.dart';

/// 用户状态管理
class UserRoom = _UserRoom with _$UserRoom;

abstract class _UserRoom with Store {
  /// 当前登录用户
  @observable
  UserPrint? currentUser;

  /// 所有用户列表
  @observable
  ObservableList<UserPrint> users = ObservableList<UserPrint>();

  /// 是否已登录
  @computed
  bool get isLoggedIn => currentUser != null;

  /// 初始化默认用户数据
  @action
  void initializeDefaultUsers() {
    users = ObservableList<UserPrint>.of([
      UserPrint(
        id: '1',
        account: 'admin',
        password: '123456',
        username: 'Ronan',
        birthDate: DateTime(1995, 3, 15),
        signature: 'Exploring the secrets of every lake.',
        avatar: 'assets/images/avatar1.png',
      ),
      UserPrint(
        id: '2',
        account: 'waterlover',
        password: '123456',
        username: 'Caleb',
        birthDate: DateTime(1992, 7, 22),
        signature: 'Lake water like a mirror.',
        avatar: 'assets/images/avatar2.png',
      ),
      UserPrint(
        id: '3',
        account: 'lakephoto',
        password: '123456',
        username: 'Evan',
        birthDate: DateTime(1988, 11, 8),
        signature:
            'Capturing every moment of the lake with my lens, making beauty eternal.',
        avatar: 'assets/images/avatar3.png',
      ),
      UserPrint(
        id: '4',
        account: 'naturefan',
        password: '123456',
        username: 'Louis',
        birthDate: DateTime(1996, 5, 30),
        signature:
            'Finding inner peace by the lake, feeling the gifts of nature.',
        avatar: 'assets/images/avatar4.png',
      ),
      UserPrint(
        id: '5',
        account: 'lakeexplorer',
        password: '123456',
        username: 'Caleb',
        birthDate: DateTime(1990, 9, 12),
        signature: 'Every lake has its story, I am willing to be the listener.',
        avatar: 'assets/images/avatar5.png',
      ),
      UserPrint(
        id: '6',
        account: 'waterdream',
        password: '123456',
        username: 'Alden',
        birthDate: DateTime(1993, 1, 25),
        signature:
            'In the beauty of lakes and mountains, finding my own piece of tranquility',
        avatar: 'assets/images/avatar6.png',
      ),
      UserPrint(
        id: '7',
        account: 'laketravel',
        password: '123456',
        username: 'Milo',
        birthDate: DateTime(1987, 6, 18),
        signature:
            'Traveling through mountains and rivers, just to meet the most beautiful lakes',
        avatar: 'assets/images/avatar7.png',
      ),
      UserPrint(
        id: '8',
        account: 'waterpeace',
        password: '123456',
        username: 'Julian',
        birthDate: DateTime(1994, 4, 3),
        signature: 'Crystal clear lake water, pure as my inner self',
        avatar: 'assets/images/avatar8.png',
      ),
      UserPrint(
        id: '9',
        account: 'lakebeauty',
        password: '123456',
        username: 'Zoe',
        birthDate: DateTime(1991, 8, 14),
        signature:
            'In the embrace of lakes, feeling the beauty and tranquility of life',
        avatar: 'assets/images/avatar9.png',
      ),
      UserPrint(
        id: '10',
        account: 'waterwisdom',
        password: '123456',
        username: 'Liam',
        birthDate: DateTime(1989, 12, 7),
        signature: 'Lakes teach me tolerance and how to stay calm in the waves',
        avatar: 'assets/images/avatar10.png',
      ),
    ]);
  }

  /// 用户登录
  @action
  bool performLogin(String account, String password) {
    try {
      final user = users.firstWhere(
        (user) => user.account == account && user.password == password,
      );
      currentUser = user;
      return true;
    } catch (e) {
      return false;
    }
  }

  /// 用户注册
  @action
  bool performRegister({
    required String account,
    required String password,
    required String username,
    required DateTime birthDate,
    required String signature,
    required String avatar,
  }) {
    try {
      // 检查账号是否已存在
      if (users.any((user) => user.account == account)) {
        return false;
      }

      // 创建新用户
      final newUser = UserPrint(
        id: (users.length + 1).toString(),
        account: account,
        password: password,
        username: username,
        birthDate: birthDate,
        signature: signature,
        avatar: avatar,
      );

      users.add(newUser);
      currentUser = newUser;
      return true;
    } catch (e) {
      return false;
    }
  }

  /// 用户退出登录
  @action
  void performLogout() {
    currentUser = null;
  }

  /// 获取当前登录用户
  UserPrint? fetchCurrentUser() {
    return currentUser;
  }

  /// 根据ID查询用户信息
  UserPrint? fetchUserById(String userId) {
    try {
      return users.firstWhere((user) => user.id == userId);
    } catch (e) {
      return null;
    }
  }

  /// 根据账号查询用户信息
  UserPrint? fetchUserByAccount(String account) {
    try {
      return users.firstWhere((user) => user.account == account);
    } catch (e) {
      return null;
    }
  }

  /// 更新用户信息
  @action
  bool updateUserInfo(UserPrint updatedUser) {
    try {
      final index = users.indexWhere((user) => user.id == updatedUser.id);
      if (index != -1) {
        users[index] = updatedUser;
        if (currentUser?.id == updatedUser.id) {
          currentUser = updatedUser;
        }
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  /// 获取所有用户列表
  List<UserPrint> fetchAllUsers() {
    return List.from(users);
  }
}
